package org.xielei.ocr;

import com.alibaba.druid.util.Base64;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.ocr.v1.OcrClient;
import com.huaweicloud.sdk.ocr.v1.model.BusinessLicenseRequestBody;
import com.huaweicloud.sdk.ocr.v1.model.RecognizeBusinessLicenseRequest;
import com.huaweicloud.sdk.ocr.v1.model.RecognizeBusinessLicenseResponse;
import com.huaweicloud.sdk.ocr.v1.region.OcrRegion;
import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class HuaweiOcr {


    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        File sourcePath = new File("C:\\Users\\leo.xie\\Desktop\\Lark20210819-111218-1.jpg");
        String targetPath = "C:\\Users\\leo.xie\\Desktop\\aaa.jpg";
        Thumbnails.of(sourcePath)
                .scale(0.3f)
                .outputQuality(0.4f)
                .outputFormat("jpg")
                .toFile(targetPath);
        File file = new File(targetPath);
        String s = Base64.byteArrayToBase64(Files.readAllBytes(file.toPath()));
        System.out.println(s);

//        new Thread(() -> getResult(s)).start();

        long end = System.currentTimeMillis();
        System.out.println(end - start);
        getResult(s);

    }

    private static String getResult(String s) {
        String ak = "8MG0Q8E5GYMKMH7GV4DK";
        String sk = "kW4M52Rz6jWEtf9kz7Y7RAr3eHDlGzEWQBBF5rtR";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        OcrClient client = OcrClient.newBuilder()
                .withCredential(auth)
                .withRegion(OcrRegion.valueOf("cn-north-4"))
                .build();

        RecognizeBusinessLicenseRequest request = new RecognizeBusinessLicenseRequest();
        BusinessLicenseRequestBody requestBody = new BusinessLicenseRequestBody();
        requestBody.withImage(s);
        request.setBody(requestBody);

        String result = null;
        try {
            RecognizeBusinessLicenseResponse response = client.recognizeBusinessLicense(request);
            result = response.getResult().toString();
            System.out.println(result);
            System.out.println("ocr result: " + response.getResult().getName() + "," + response.getResult().getRegistrationNumber());
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return result;
    }


}
